using System;
using System.Collections.Generic;

namespace AsyncAwaitDemo
{
    public class RomanToNum
    {
        /**
         *解一：利用hash表
         * 
         */
        public static int RomanToInt(string s)
        {
            int result = 0;
            Dictionary<string,int> m_Romans = new Dictionary<string, int>()
            {
                {"I",1},
                {"IV",4},
                {"V",5},
                {"IX",9},
                {"X",10},
                {"XL",40},
                {"L",50},
                {"XC",90},
                {"C",100},
                {"CD",400},
                {"D",500},
                {"CM",900},
                {"M",1000},
            };

            for (int i = 0; i < s.Length; i++)
            {
                int cur = 0;
                if (m_Romans.ContainsKey(s[i].ToString()))
                {
                    cur = m_Romans[s[i].ToString()];
                }

                if (i+1<s.Length && s[i+1].ToString() !="I")
                {
                    string next = s[i].ToString() + s[i + 1].ToString();
                    if (m_Romans.ContainsKey(next))
                    {
                        cur = m_Romans[next];
                        i++;
                    }
                }
                result += cur;
            }

            return result;
        }
    }
}